package com.baizhi.zjs.service;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.apache.struts2.ServletActionContext;

import com.baizhi.zjs.dao.FUserDAO;
import com.baizhi.zjs.entity.FUSerEntity;
import com.baizhi.zjs.util.Md5Utils;
import com.baizhi.zjs.util.MybatisUtil;

public class FrontUserServiceImpl implements FrontUserServicee{
//用户添加的service
	@Override
	public void addFUser(FUSerEntity user) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		FUserDAO dao = sqlSession.getMapper(FUserDAO.class);
		try {
			dao.addUser(user);
			MybatisUtil.commit(sqlSession);
			MybatisUtil.close(sqlSession);
			
		} catch (Exception e) {
			// TODO: handle exception
			MybatisUtil.rollback(sqlSession);
			MybatisUtil.close(sqlSession);
			e.printStackTrace();
		}
		
		
	}

	@Override
	public String login(FUSerEntity user) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		FUserDAO dao = sqlSession.getMapper(FUserDAO.class);
		try {
			
			//判断用户名是不是空
			
				FUSerEntity logi = dao.login(user.getEmail());
				//判断账号是否被冻结
				if(logi.getStatus()==0){
					return "dongError";
				}
				
				//判断密码是不是空
				/*if(user.getPassword()==null){
					return "passwordNull";
				}*/
				//判断密码是不是相等
				//调用MD5工具类
				Md5Utils md5Utils = new Md5Utils();
				String code=md5Utils.getMd5Code(user.getPassword()+logi.getSalt());
				System.out.println(logi.getPassword());
				System.out.println(code);
				if(code.equals(logi.getPassword())){
					HttpSession session = ServletActionContext.getRequest().getSession();
					//设置登录标志，将用户的信息存入session中
					session.setAttribute("login", logi);
					return "ok";
				}
			
			
				return "Error";
			
		
		} catch (Exception e) {
			// TODO: handle exception
			//找不到账号
			return "emailError";
		}
		//return "Error";
	}
//邮箱查重
	@Override
	public String showOne(String em) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		FUserDAO dao = sqlSession.getMapper(FUserDAO.class);
		try {
			FUSerEntity login = dao.login(em);
			if(login ==null){
				return "ok";
			}
			return "emError";
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "emError";
		}
	}
	//=============================后台查询用户===
	@Override
	public List<FUSerEntity> showAllUser() {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		FUserDAO dao = sqlSession.getMapper(FUserDAO.class);
		List<FUSerEntity> list = dao.showAllUser();
		MybatisUtil.close(sqlSession);
		return list;
	}

	@Override
	public void updateUser(String em) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		FUserDAO dao = sqlSession.getMapper(FUserDAO.class);
		FUSerEntity login = dao.login(em);
		if(login.getStatus()==1){
			login.setStatus(0);
		}else {
			login.setStatus(1);
		}
		
		dao.updateUser(login);
		MybatisUtil.commit(sqlSession);
	}

}
